var source = require('./source/testSource.js');
var async = require('async');
var request = require('request');
var schedule = require('node-schedule');
//var readability = require('node-readability-cheerio');
var cheerio = require('cheerio');
var mysqlDao = require('./dao/mysqlDao.js');
function timeShift(utcStr, tz) {
    var time = new Date(new Date(utcStr).getTime() + tz * 3600000);
    var obj = {
        year: time.getUTCFullYear(),
        month: (time.getUTCMonth() + 1),
        date: time.getUTCDate(),
        hh: time.getUTCHours(),
        mm: time.getUTCMinutes()
    };
    for (var x in obj) {
        if (obj[x] < 10) {
            obj[x] = '0' + obj[x];
        }
    }

    time = obj.year + '-' + obj.month + '-' + obj.date + ' ' +
        obj.hh + ':' + obj.mm + ' UTC';

    //var sign, hour, min;
    //if (tz < 0) {
    //    sign = '-';
    //    tz = -tz;
    //}
    //else {
    //    sign = '+';
    //}
    //if (tz >= 10) {
    //    hour = '' + Math.floor(tz);
    //}
    //else {
    //    hour = '0' + Math.floor(tz);
    //}
    //if (Math.floor(tz) === tz) {
    //    min = '00';
    //}
    //else {
    //    min = '30';
    //}
    return time.replace(' UTC', '');
    //return time + sign + hour + min;
}
function getQuotesFromInvesting(symbols, callback) {
    //try {
    //    var investing = [];
    //    for (var j = 0; j < symbols.length; j++) {
    //        var obj = map.search(symbols[j]);
    //        if (obj != undefined && obj.investingURL != undefined) {
    //            investing.push(obj);
    //        }
    //    }
    //    if (investing.length === 0) {
    //        return callback(null, []);
    //    }
    //}
    //catch (e) {
    //    return callback(e + ' getQuotesFromInvesting', null);
    //}

    async.mapLimit(symbols, 2, function (item, callback) {
        getQuote(item, function (err, quote) {
            if (err) return callback(err, null);
            callback(null, quote);
        });
    }, function (err, quotes) {
        if (err) {
            callback(err);
            return;
        }
        var quoteArray = [];
        for (var j = 0; j < quotes.length; j++) {
            if (typeof quotes[j] != 'string') {
                quoteArray.push(quotes[j]);
            }
        }
        callback(null, quoteArray);
    });

    function getQuote(obj, callback) {
        var url = obj.investingUrl;
        if (url.indexOf('http://') != -1) {
            url=url.replace('http://','https://')
        }
        var options = {
            url: url,
            headers: {
                'Host': 'www.investing.com',
                'Referer': 'http://www.investing.com/etfs/deutsche-x-trackers-harvest-csi-500',
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 ' +
                '(KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36',
                'X-Requested-With': 'XMLHttpRequest'
            }
        };
        request(options, function (err, res, body) {
            if (err) {
                console.error(err + ' in getQuotesFromInvesting');
                return callback(null, obj.symbol);
            }
            //console.log(options.url);
            try {
                var $ = cheerio.load(body);
                var data = $('.overviewDataTable > div span');
                var previousclose, volume, range, frange, open, last, high, fhigh, low, flow, change;

                // get parameters
                last = $('#last_last').text();
                change = $('#last_last+span').text();
                for (var j = 0; j < data.length; j++) {
                    if ($(data[j]).text().indexOf('Prev') != -1 && $(data[j]).text().indexOf('Close') != -1) {
                        previousclose = $(data[j]).next().text();
                    }
                    else if ($(data[j]).text().indexOf('Day') != -1 && $(data[j]).text().indexOf('Range') != -1) {
                        range = $(data[j]).next().text();
                    }
                    else if ($(data[j]).text().indexOf('52') != -1 && $(data[j]).text().indexOf('Range') != -1) {
                        frange = $(data[j]).next().text();
                    }
                    else if ($(data[j]).text().indexOf('Volume') != -1) {
                        volume = $(data[j]).next().text();
                    }
                    else if ($(data[j]).text().indexOf('Open') != -1) {
                        open = $(data[j]).next().text();
                    }
                }
                // check parameters
                if (previousclose === undefined || previousclose === null ||
                    last === undefined || last === null) {
                    console.log('getQuotesFromInvesting: previousclose or last is undefined or null ' + obj.symbol);
                    return callback(null, obj.symbol);
                }
                else {
                    last = Number(last.replace(/,/g, ''));
                    change = Number(change.replace(/,/g, ''));
                    previousclose = Number(previousclose.replace(/,/g, ''));
                    if (isNaN(last) || isNaN(previousclose) || isNaN(change)) {
                        console.log('getQuotesFromInvesting: previousclose or last or change is NaN ' + obj.symbol);
                        return callback(null, obj.symbol);
                    }
                }
                if (range === undefined || range === null || range.indexOf('-') === -1) {
                    high = null;
                    low = null;
                }
                else {
                    range = range.replace(/,/g, '').split('-');
                    low = range[0].trim();
                    high = range[1].trim();
                    if (isNaN(Number(low)) || isNaN(Number(high))) {
                        low = null;
                        high = null;
                    }
                }
                if (frange === undefined || frange === null || frange.indexOf('-') === -1) {
                    fhigh = null;
                    flow = null;
                }
                else {
                    frange = frange.replace(/,/g, '').split('-');
                    flow = frange[0].trim();
                    fhigh = frange[1].trim();
                    if (isNaN(Number(flow)) || isNaN(Number(fhigh))) {
                        flow = null;
                        fhigh = null;
                    }
                }

                if (volume === undefined || volume === null) {
                    volume = '';
                }
                else {
                    volume = volume.replace(/,/g, '');
                    volume = Number(volume);
                    if (isNaN(volume)) {
                        volume = '';
                    }
                    else if (volume >= 1000000000) {
                        volume = Math.round(volume / 10000000) / 100 + 'B';
                    }
                    else if (volume >= 1000000) {
                        volume = Math.round(volume / 10000) / 100 + 'M';
                    }
                    else if (volume >= 1000) {
                        volume = Math.round(volume / 10) / 100 + 'K';
                    }
                    else {
                        volume = volume + '';
                    }
                }
                if (open === undefined || open === null) {
                    open = null;
                }
                else {
                    open = Number(open.replace(/,/g, ''));
                    if (isNaN(open)) {
                        open = null;
                    }
                }
            }
            catch (e) {
                console.log(e + ' getQuotesFromInvesting: fail to get key parameters' + obj.symbol);
                return callback(null, obj.symbol);
            }

            try {
                // time and market state;
                var time = $('#last_last').parent().next().text().toLowerCase().trim();
                var redClockIcon = $('.inlineblock .redClockBigIcon');
                var closed;
                var tempTime1 = time.match(/[0-9]{1,2}\/[0-9]{1,2}/g),  // dd/mm
                    tempTime2 = time.match(/[0-9]{1,2}:[0-9]{1,2}/g);   // hh:mm
                if (tempTime1 === null && tempTime2 != null) {  // hh:mm
                    closed = false;
                    time = tempTime2[0];
                }
                else if (tempTime1 != null && tempTime2 === null) { // dd/mm
                    closed = true;
                    time = tempTime1[0];
                }
                else {
                    console.error('Error time format in getQuotesFromInvesting: ' +
                        obj.symbol + ' - ' + time);
                    return callback(null, obj.symbol);
                }

                if (typeof time === 'string' && time.toLowerCase().indexOf('closed') != -1) {
                    closed = true;
                }
                if (redClockIcon.length === 1) {
                    closed = true;
                }

                var quote = {
                    country: obj.country,
                    symbol: obj.symbol,
                    name: obj.name_cn,
                    last: last,
                    open: open,
                    high: high,
                    '52_high': fhigh,
                    low: low,
                    '52_low': flow,
                    volume: volume,
                    previousclose: previousclose,
                    change: change,
                    changeinpercent: Math.round(change / (last - change) * 10000) / 100,
                    time: time,
                    ytd: '',
                    realtime: 'false',
                    closed: closed,
                    src: 'investing'
                };

                // convert utc time to local time
                var curr = new Date();
                var possibleNewYear = new Date(curr.getTime() + 3600000 * 12).getUTCFullYear();
                var possibleOldYear = new Date(curr.getTime() - 3600000 * 12).getUTCFullYear();
                if (tempTime1 != null && tempTime2 === null) { // time format: dd/mm
                    time = time.split('/');
                    if (time[1] === '12') {
                        quote.time = possibleOldYear + '-' + time[1] + '-' + time[0];
                    }
                    else {
                        quote.time = possibleNewYear + '-' + time[1] + '-' + time[0];
                    }

                } else {
                    time = curr.getUTCFullYear() + '-' +
                        (curr.getUTCMonth() + 1) + '-' +
                        curr.getUTCDate() + ' ' + time + ' UTC+0000';
                    //quote.time = timeShift(time, obj.tz || 0);
                    // 时区减四
                    // quote.time = timeShift(time, -4);
                    // 夏令时减五
                    quote.time = timeShift(time, -5);
                }
                // adjust future date
                if (new Date(quote.time) - new Date() > 3600000 * 24) {
                    quote.time = quote.time.replace(/[0-9]{4}/, possibleNewYear - 1);
                }
            }
            catch (e) {
                console.error(e + ' in getQuotesFromInvesting: ' + obj.symbol);
                return callback(null, obj.symbol);
            }
            callback(null, quote);
        });
    }
}
//var fs = require('fs');
//getQuotesFromInvesting(source, function (err, data) {
//    if (err) {
//        console.log(err);
//        return;
//    }
//    console.log(data);
//    var l = data.length;
//    console.log(l);
//    for (var i = 0; i < l; i++) {
//        console.log(data[i]);
//        if (data[i].symbol == 'SPY') {
//            var data = 'spy:' + data[i].time;
//            var writerStream = fs.createWriteStream('output.txt');
//            writerStream.write(data, "UTF-8");
//            writerStream.end();
//            writerStream.on('finish', function () {
//                console.log("写入完成");
//            });
//            writerStream.on('error', function (err) {
//                console.log(err.stack);
//            });
//            console.log("execute end...");
//        }
//    }
//    //mysqlDao.toRealTime(data);
//});
var rule = new schedule.RecurrenceRule();
var times = [];
var l = 60;
for (var i = 0; i < l; i++) {
    times.push(i);
}
rule.minute = times;
var j = schedule.scheduleJob(rule, function () {
    console.log('XXXXXXXXX 开始' + new Date().toString());
    var date = new Date().getTime() + 8 * 3600000;
    var now=new Date(date);
    var hour=Number(now.getHours());
    var minute=Number(now.getMinutes());
    if((hour==5&&minute>=30)||(hour>5&&hour<=21)){
        console.log('XXXXXXXXX 不在轮训时间，北京时间->'+hour+':'+minute);
        console.log('XXXXXXXXX 结束' + new Date().toString());
        return;
    }
    getQuotesFromInvesting(source.slice(****, ####),
    function (err, data) {
        if (err) {
            console.log(err);
            return;
        }
        console.log('XXXXXXXXX 结束' + new Date().toString());
        mysqlDao.toRealTime(data);
    });
});